/**
 * Created by Churcy on 2017/1/9.
 */

$(function () {
    Employee.searchEmployee();
});

var Employee = {

    searchEmployee: function () {
        $("#searchBTN").on('click', function () {
            var conditions = getCurrentConditions();
            conditions.pageNo = 1;
            ajaxSearchEmployeeList(conditions);
        });
        $("#resetBTN").on('click', function () {
            resetFn();
        });
    }
}
function searchFn() {
    var conditions = getCurrentConditions();
    console.log("searchFN")
    var response = ajaxSearchEmployeeList(conditions);
}
// 重置查询条件
function resetFn() {
    $('#empCode').val("");
    $('#empName').val("");
    $('#companyName').val("");
    $('#deptName').val("");

}

function getCurrentConditions() {

    var pageNo = $("#p").val();
    var empCode = $('#empCode').val();
    var empName = $('#empName').val();
    var deptName = $('#deptName').val();
    var companyName = $('#companyName').val();
    var APP_KEY = $('#APP_KEY').val();
    var conditions = {
        empCode: empCode,
        empName: encodeURI(empName),
        deptName: encodeURI(deptName),
        companyName: encodeURI(companyName),
        appKey:APP_KEY,
        pageIndex: pageNo
    };
    return conditions;

}
function ajaxSearchEmployeeList(conditions) {
    console.log("ajaxSearchHospitalList");
    $.ajax({
        type: "GET",
        url: PROJECT_CTX + "/trafficback/employeeList",
        data: conditions,
        dataType: "json",
        success: function (data) {
            console.log(data)
            var result = eval(data);
            var resultCode = result.status.code;
            console.log(resultCode)
            console.log(result.line);
            if (1 == resultCode) {
                // 清空当前数据
                var employeeList = $("#employeeList").empty();
                // 填写查询出来的数据
                $.each(result.line, function (i, item) {
                    var tr = "<tr>";

                    tr += "<td >";
                    tr += item.empCode;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.empName;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.gender;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.telephone;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.deptName;
                    tr += "</td>"

                    tr += "<td >";
                    if (item.companyName == null) {
                        tr += '暂无数据' ;
                    } else {
                        tr += item.companyName;
                    }

                    tr += "</td>"

                    /*tr += "<td >";
                     tr += " <a title='修改' href='#'>修改</a>";
                     tr += " <a title='删除' href='#'>删除</a>";
                     tr += "</td >";*/

                    tr += "</tr>";
                    employeeList.append(tr);
                    // 更新分页栏
                    reloadPage(result);
                });
            }

        }

        ,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}

function formatDate(date) {
    var date = new Date(date);
    var month = date.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    var today = date.getDate();
    if (today < 10) {
        today = "0" + today;
    }
    return date.getFullYear() + "-" + month + "-" + today;
}